<template>
    <div class="person">
      <h3>当前的人数为:{{total}}</h3>
      <input type="text" placeholder="请输入名字" v-model="name"><button @click="addPerson">添加</button>
      <ul>
          <li v-for="person in personList" :key="person.id">{{person.name}}</li>
      </ul>
    </div>
</template>

<script>
    import {mapState,mapGetters} from 'vuex'
    export default {
    name:'Person',
    data() {
        return {
            name:''
        }
    },
    computed: {
        ...mapState('person',['personList']),
        ...mapGetters('person',['total'])
    },
    methods:{
        addPerson(){
            const personObj = {id:Date.now(),name:this.name}
            this.$store.dispatch('person/add',personObj)
        }
    }
  }
</script>

<style scoped>
  .person{
      background-color: skyblue;
      padding: 20px;
  }
</style>